package com.fzzz.kotlindemo;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import java.util.*;

/**
 * Created by Shen Chao
 * Created by 2018-12-26
 */
public class GuavaDemo {

    public static void main(String[] args) {
//        test6();
        demo2();
    }

    private static void demo2() {
        Optional<Integer> sum = sum(Optional.fromNullable(3), Optional.<Integer>absent());
        if (sum.isPresent()) {
            System.out.println(sum.get());
        } else {
            System.out.println("参数错误");
        }
    }

    private static void demo1() {
        //one
        Optional<Integer> possible = Optional.fromNullable(5);  //创建允许null值的Optional
        //two
        Integer nubmerone = 4;
        Optional<Integer> integerOptional = Optional.of(nubmerone);//若引用为null则快速失败触发java.lang.NullPointerException
        //three
        Optional<Integer> nullOptional = Optional.absent();//创建引用缺失的Optional实例,就是为NULL的

        List<String> list = ImmutableList.of("a","b","c");
        Map<String, String> map = ImmutableMap.of("firstName", "John", "lastName", "Doe");
    }

    private static void test1() {
        Optional<Integer> possible = Optional.fromNullable(5);  //创建允许null值的Optional
        if (possible.isPresent()) {//包含的引用非null的（引用存在），返回true
            System.out.println("possible.value：" + possible.get());//包含的引用缺失(null)，则抛出java.lang.IllegalStateException   //5
        } else {
            System.out.println("possible is null");
        }
    }

    private static void test2() {
        Optional<Integer> possible = Optional.fromNullable(null);  //创建允许null值的Optional
        if (possible.isPresent()) {//包含的引用非null的（引用存在），返回true
            System.out.println("possible.value：" + possible.get());//包含的引用缺失(null)，则抛出java.lang.IllegalStateException
        } else {
            System.out.println("possible is null"); //print
        }
    }

    private static void test3() {
        Optional<Integer> optional1 = Optional.absent();//null就是缺少的意思
        Integer value = optional1.or(3);
        System.out.println("【if optional1 is null,return is 3,others is value】value =" + value);    //3

        Optional<Integer> optional2 = Optional.of(5);
        Integer value2 = optional2.or(3);
        System.out.println("【if optional2 is null,return is 3,others is value】value =" + value2);   //5
    }

    private static void test4() {
        Optional<Integer> nulloptional = Optional.absent();//null就是缺少的意思
        Integer value1 = nulloptional.orNull();
        System.out.println("value1 = " + value1);   //null

        Optional<Integer> optional = Optional.of(5);
        Integer value2 = optional.orNull();
        System.out.println("value2 = " + value2);   //5
    }

    private static void test5() {
        Optional<Integer> nulloptional = Optional.absent();//null就是缺少的意思
        Set<Integer> set1 = nulloptional.asSet();
        System.out.println("set1 size = " + set1.size()); //0

        Optional<Integer> optional = Optional.of(5);
        Set<Integer> set2 = optional.asSet();
        System.out.println("set2 size = " + set2.size()); //1
    }

    //求和
    private static Optional<Integer> sum(Optional<Integer> a, Optional<Integer> b) {
        if (a.isPresent() && b.isPresent()) {
            return Optional.of(a.get() + b.get());
        }
        return Optional.absent();
    }

    private static void test6() {
        Objects.equal("a", "a");     // returns true
        Objects.equal(null, "a");    // returns false
        Objects.equal("a", null);    // returns false
        Objects.equal(null, null);   // returns true
    }

}
